package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderStatisticsVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 新增订单
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     * 用于替换微信支付更新数据库状态的问题
     * @param orderStatus
     * @param orderPaidStatus
     */
    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, String orderNumber);


    /**
     * 查询所有订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单ID查询订单
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getByOrderById(Long id);

    /**
     * 统计不同状态订单数量
     * @return
     */
    OrderStatisticsVO statistics();

    @Select(" select * from orders where status = #{status} and order_time < #{check_out_time}")
    List<Orders> getByStatusAndOrderTime(Integer status, LocalDateTime check_out_time);

    /**
     * 根据开始和结束日期查询营业额
     * @param begin
     * @param end
     * @return
     */
    List<Orders> getByTime(Integer status,LocalDate begin, LocalDate end);


    /**
     * 查询当填营业额
     * @param now
     * @return
     */
    Double getTurnoverByDate(LocalDate now);

    /**
     * 根据条件查询某日订单数
     * @param status
     * @param now
     * @return
     */
    Integer getCountOrdersNumber(Integer status, LocalDate now);

    OrderOverViewVO getOverviewOrders();
}
